package com.huayue.point.controller;

import com.huayue.point.api.PointServiceApi;
import com.huayue.point.bean.PointDto;
import com.huayue.point.bean.RankDto;
import com.huayue.user.bean.UserDto;
import com.huayue.user.exception.ParamException;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Collection;

@RestController
@RequestMapping("point")
public class PointController implements BaseController {
    @Reference
    private PointServiceApi pointServiceApi;
    /**
     * 添加积分
     * @param request
     * @param point
     * @param no
     * @param mark
     * @return
     * @throws Exception
     */
    @RequestMapping("add")
    public String add(HttpServletRequest request, Integer point, String no, String mark) throws Exception {
        checkAdmin(request, "您不是管理员，不允许添加积分。");
        pointServiceApi.add(point, Arrays.asList(no.split(",")), mark);
        return "积分添加成功。";
    }

    /**
     * 查询积分明细
     * @param no
     * @return
     */
    @RequestMapping("detail")
    Collection<PointDto> queryDetail(HttpServletRequest request, @RequestParam(required = false) String no) throws Exception {
        if (StringUtils.isNotEmpty(no)) {
            checkAdmin(request, "您不是管理员，不允许查看他人的积分明细。");
        } else {
            UserDto user = (UserDto)request.getSession().getAttribute("user");
            no = user.getNo();
        }
        return pointServiceApi.queryDetail(no);
    }

    /**
     * 查询个人总积分
     * @param no
     * @return
     */
    @RequestMapping("sum")
    Integer querySum(HttpServletRequest request, @RequestParam(required = false) String no) throws Exception {
        if (StringUtils.isNotEmpty(no)) {
            checkAdmin(request, "您不是管理员，不允许查看他人的总积分。");
        } else {
            UserDto user = (UserDto)request.getSession().getAttribute("user");
            no = user.getNo();
        }
        return pointServiceApi.querySum(no);
    }

    /**
     * 积分排名
     * @return
     */
    @RequestMapping("rank")
    Collection<RankDto> rank() throws ParamException {
        return pointServiceApi.rank();
    }

    /**
     * 修改积分
     * @return
     */
    @RequestMapping("update")
    public String update(HttpServletRequest request, Integer id, Integer point) throws Exception {
        checkAdmin(request, "您不是管理员，不允许修改积分。");
        Boolean update = pointServiceApi.update(id, point);
        if (update) {
            return "积分修改成功。";
        }  else {
            throw new Exception("积分id不存在。");
        }
    }

    /**
     * 删除积分
     * @param id
     * @return
     */
    @RequestMapping("delete/{id}")
    public String delete(HttpServletRequest request, @PathVariable Integer id) throws Exception {
        checkAdmin(request, "您不是管理员，不允许删除积分。");
        Boolean delete = pointServiceApi.delete(id);
        if (delete) {
            return "积分删除成功。";
        }  else {
            throw new Exception("积分id不存在。");
        }
    }
}
